package moreApi.src.arrays;

import regex.Student;

import java.util.Arrays;
import java.util.Comparator;

/**
 * 自定义比较器排序
 */
public class ArraysDemo2 {
    public static void main(String[] args) {
        int[] ages = {34,12,42,23};
        //sort默认是对有值属性的数据直接升序排序
        Arrays.sort(ages);
        System.out.println(Arrays.toString(ages));

        //sort比较器只能用与引用类型排序
        Integer[] age2 = {34,12,42,23};
        Arrays.sort(age2, new Comparator<Integer>() {
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2-o1;//反向定义<号就可以降序
            }
        });
        System.out.println(Arrays.toString(age2));
        System.out.println("----------------------------");
        Student[] ss = new Student[3];
        ss[0] = new Student(18,185,63,"刘大脑袋");
        ss[1] = new Student(19,181,61,"王大国");
        ss[2] = new Student(20,178,58,"香秀");
        System.out.println(Arrays.toString(ss));

        //自定义类的比较器
        Arrays.sort(ss, new Comparator<Student>() {
            @Override
            public int compare(Student o1, Student o2) {
//                return (int)o1.getHeight()-(int)o2.getHeight();
                return Double.compare(o1.getHeight(),o2.getHeight());//如果直接double相减再强转的话,如果差值小于1会被判定等于,所以要用double的比较方法
            }
        });
        System.out.println(Arrays.toString(ss));
    }

}
